package gamecenter.gc.controller;

import gamecenter.gc.bean.Administrator;
import gamecenter.gc.bean.Comment;
import gamecenter.gc.bean.Game;
import gamecenter.gc.bean.Game_Developer;
import gamecenter.gc.bean.Refund_Request;
import gamecenter.gc.service.Administrator_Service;
import gamecenter.gc.service.Comment_Service;
import gamecenter.gc.service.Game_Center_Service;
import gamecenter.gc.service.Game_Developer_Service;
import gamecenter.gc.service.Game_Service;
import gamecenter.gc.service.Refund_Request_Service;
import gamecenter.gc.service.User_Game_Service;
import gamecenter.gc.service.User_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Administrator_Controller {
    @Autowired
    private Game_Center_Service game_center_service;
    @Autowired
    private Game_Service game_service;
    @Autowired
    private Administrator_Service administrator_service;
    @Autowired
    private Game_Developer_Service game_developer_service;
    @Autowired
    private Refund_Request_Service refund_request_service;
    @Autowired
    private User_Game_Service user_game_service;
    @Autowired
    private  User_Service user_service;
    @Autowired
    private Comment_Service comment_service;

    @RequestMapping(value = "register_administrator", method = RequestMethod.POST)
    public boolean register_Administrator(@RequestBody Administrator administrator) {
        String name = administrator.getName();
        String password = administrator.getPassword();
        return administrator_service.register(name, password);
    }

    @RequestMapping(value = "cer_administrator", method = RequestMethod.GET)
    public Administrator get_Cer_Administrator(String name, String password) {
        if (administrator_service.check_Administrator(name, password))
            return administrator_service.get_Cer_Administrator_From_Name(name);
        return null;
    }

    @RequestMapping(value = "modify_administrator_password", method = RequestMethod.PUT)
    public boolean modify_Administrator_Password(@RequestBody Administrator administrator, String n_password) {
        String name = administrator.getName();
        String o_password = administrator.getPassword();
        if (!administrator_service.check_Administrator(name, o_password))
            return false;
        administrator_service.modify_Administrator_Password(name, n_password);
        return true;
    }

    @RequestMapping(value = "modify_administrator_photo", method = RequestMethod.PUT)
    public boolean modify_Administrator_Photo(@RequestBody Administrator administrator) {
        String name = administrator.getName();
        String password = administrator.getPassword();
        String photo = administrator.getPhoto();
        if (!administrator_service.check_Administrator(name, password))
            return false;
        administrator_service.modify_Administrator_Photo(name, photo);
        return true;
    }

    @RequestMapping(value = "on_Cer_Game", method = RequestMethod.PUT)
    public boolean on_Cer_Game(@RequestBody Administrator administrator, String game_name) {
        String a_name = administrator.getName();
        String password = administrator.getPassword();
        if (!administrator_service.check_Administrator(a_name, password))
            return false;
        game_service.on_Cer_Game(game_name);
        return true;
    }

    @RequestMapping(value = "add_Developer", method = RequestMethod.POST)
    public boolean add_Game_Developer(String a_name, String password, @RequestBody Game_Developer game_developer) {
        if (!administrator_service.check_Administrator(a_name, password))
            return false;
        if (game_developer_service.get_Game_Developer_From_Name(game_developer.getName()) != null)
            return false;
        game_developer_service.add_Game_Developer(game_developer);
        return true;
    }

    @RequestMapping(value = "process_Refund", method = RequestMethod.DELETE)
    public boolean process_Refund_Request(String user_name, String game_name, boolean success, String admin_name, String password) {
        Refund_Request refund_request = refund_request_service.get_Cer_Request(user_name, game_name);
        if (!administrator_service.check_Administrator(admin_name, password))
            return false;
        if (!refund_request_service.check_Request(refund_request.getUser_name(), refund_request.getGame_name()))
            return false;
        refund_request.setSuccess(success);
        if (refund_request.isSuccess()) {
            refund_request_service.del_Request(refund_request);
            user_game_service.del_Game(user_name, game_name);
            Game game = game_service.get_Cer_Game_From_Name(game_name);
            refund_request.setMoney(game.getPrice());
            user_service.user_Recharge(user_name, refund_request.getMoney());
        }
        else {
            refund_request_service.del_Request(refund_request);
        }
        return true;
    }

    @RequestMapping(value = "get_all_Refund", method = RequestMethod.GET)
    public List<Refund_Request> get_all_Refund() {
        return refund_request_service.get_all_Request();
    }



    @RequestMapping(value = "del_Comment_admin", method = RequestMethod.DELETE)
    public boolean admin_Del_Comment(String user_name, String game_name, String admin_name, String password) {
        if (!administrator_service.check_Administrator(admin_name, password))
            return false;
        if (!user_game_service.has_Game(user_name, game_name))
            return false;

        if (!comment_service.has_Comment(user_name, game_name)) {
            return false;
        }
        else {
            Comment comment = comment_service.get_Comment(user_name, game_name);
            comment_service.del_Comment(comment);
        }
        return true;
    }
}
